package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.common.page.LayuiPageFactory;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.util.StringUtil;
import cn.stylefeng.guns.core.util.TencentCloudUtils;
import cn.stylefeng.guns.modular.system.entity.ExamResultUser;
import cn.stylefeng.guns.modular.system.mapper.ExamResultUserMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生成绩表 服务类
 * </p>
 *
 * @author zfy
 * @since 2020-08-15
 */
@Service
public class ExamResultUserService extends ServiceImpl<ExamResultUserMapper,ExamResultUser> {
    final private String EXAM = "exam/";
    @Autowired
    private SysSchoolAgentService sysSchoolAgentService;

    public IPage<Map<String,Object>> examResultList(String name) {
        return this.baseMapper.examResultList (LayuiPageFactory.defaultPage(),
                ShiroKit.isAdmin()?null:sysSchoolAgentService.getSchoolByAgent().split(","),name);
    }

    public void resultSave (ExamResultUser resultUser){
        if(ToolUtil.isEmpty(resultUser.getExamResultUserId()) &&
                isResult(resultUser.getUserId(),resultUser.getExamId(),resultUser.getExamContentId())){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        if(ToolUtil.isNotEmpty(resultUser.getExamWork())){
            List<String> works = new ArrayList<>();
            Arrays.asList(resultUser.getExamWork().split(",")).forEach(work->{
                String cover = EXAM + StringUtil.getFileName(work);
                TencentCloudUtils.copyObject(work, cover);
                works.add(cover);
            });
            resultUser.setExamWork(String.join(",",works));
        }
        resultUser.setCreateTime(LocalDateTime.now());
        this.saveOrUpdate(resultUser);
    }

    private Boolean isResult (Long userId,Long examId,Long examContentId ){
        ExamResultUser resultUser = this.getOne(new QueryWrapper<ExamResultUser>()
                .eq("user_id", userId)
                .eq("exam_id", examId)
                .eq("exam_content_id", examContentId));
        return resultUser==null?false:true;
    }

    public Map<String,Object> examResulInfo (Long examResultUserId){
        return this.baseMapper.examResulInfo(examResultUserId);
    }

    public List<Map<String,Object>> examResultUserList (Long userId,Long examId){
        return this.baseMapper.examResultUserList ( userId, examId);
    }
}
